﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using SYH.Config;
using System.Data;
using Common;

namespace SYH.Public.DAL
{
    public class Public_AdAreaInfo
    {
        public Public_AdAreaInfo()
        { }
        #region  基本方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Public_AdAreaInfo");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            object obj = SqlHelper.ExecuteScalar(WebConfig.SqlConnString, CommandType.Text, strSql.ToString(), parameters);
            return Common.TypeParse.ObjectToInt(obj, 0) > 0;
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(SYH.Public.Model.Public_AdAreaInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Public_AdAreaInfo(");
            strSql.Append("Title,Width,Height,Template,AreaContent,CreateTime)");
            strSql.Append(" values (");
            strSql.Append("@Title,@Width,@Height,@Template,@AreaContent,@CreateTime)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Title", SqlDbType.NVarChar,20),
					new SqlParameter("@Width", SqlDbType.VarChar,20),
					new SqlParameter("@Height", SqlDbType.VarChar,20),
					new SqlParameter("@Template", SqlDbType.VarChar,100),
					new SqlParameter("@AreaContent", SqlDbType.NVarChar,100),
					new SqlParameter("@CreateTime", SqlDbType.DateTime)};
            parameters[0].Value = model.Title;
            parameters[1].Value = model.Width;
            parameters[2].Value = model.Height;
            parameters[3].Value = model.Template;
            parameters[4].Value = model.AreaContent;
            parameters[5].Value = model.CreateTime;

            int obj = SqlHelper.ExecuteNonQuery(WebConfig.SqlConnString, CommandType.Text, strSql.ToString(), parameters);
            return obj;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(SYH.Public.Model.Public_AdAreaInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Public_AdAreaInfo set ");
            strSql.Append("Title=@Title,");
            strSql.Append("Width=@Width,");
            strSql.Append("Height=@Height,");
            strSql.Append("Template=@Template,");
            strSql.Append("AreaContent=@AreaContent,");
            strSql.Append("CreateTime=@CreateTime");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Title", SqlDbType.NVarChar,20),
					new SqlParameter("@Width", SqlDbType.VarChar,20),
					new SqlParameter("@Height", SqlDbType.VarChar,20),
					new SqlParameter("@Template", SqlDbType.VarChar,100),
					new SqlParameter("@AreaContent", SqlDbType.NVarChar,100),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = model.Title;
            parameters[1].Value = model.Width;
            parameters[2].Value = model.Height;
            parameters[3].Value = model.Template;
            parameters[4].Value = model.AreaContent;
            parameters[5].Value = model.CreateTime;
            parameters[6].Value = model.Id;

            int rows = SqlHelper.ExecuteNonQuery(WebConfig.SqlConnString, CommandType.Text, strSql.ToString(), parameters);
            return rows > 0;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Public_AdAreaInfo ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            int rows = SqlHelper.ExecuteNonQuery(WebConfig.SqlConnString, CommandType.Text, strSql.ToString(), parameters);
            return rows > 0;
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Public_AdAreaInfo ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            int rows = SqlHelper.ExecuteNonQuery(WebConfig.SqlConnString, CommandType.Text, strSql.ToString());
            return rows > 0;
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SYH.Public.Model.Public_AdAreaInfo GetModel(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,Title,Width,Height,Template,AreaContent,CreateTime from Public_AdAreaInfo ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            SYH.Public.Model.Public_AdAreaInfo model = new SYH.Public.Model.Public_AdAreaInfo();
            DataSet ds = SqlHelper.ExecuteDataset(WebConfig.SqlConnString, CommandType.Text, strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SYH.Public.Model.Public_AdAreaInfo DataRowToModel(DataRow row)
        {
            SYH.Public.Model.Public_AdAreaInfo model = new SYH.Public.Model.Public_AdAreaInfo();
            if (row != null)
            {
                if (row["Id"] != null && row["Id"].ToString() != "")
                {
                    model.Id = int.Parse(row["Id"].ToString());
                }
                if (row["Title"] != null)
                {
                    model.Title = row["Title"].ToString();
                }
                if (row["Width"] != null)
                {
                    model.Width = row["Width"].ToString();
                }
                if (row["Height"] != null)
                {
                    model.Height = row["Height"].ToString();
                }
                if (row["Template"] != null)
                {
                    model.Template = row["Template"].ToString();
                }
                if (row["AreaContent"] != null)
                {
                    model.AreaContent = row["AreaContent"].ToString();
                }
                if (row["CreateTime"] != null && row["CreateTime"].ToString() != "")
                {
                    model.CreateTime = DateTime.Parse(row["CreateTime"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,Title,Width,Height,Template,AreaContent,CreateTime ");
            strSql.Append(" FROM Public_AdAreaInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SqlHelper.ExecuteDataset(WebConfig.SqlConnString, CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" Id,Title,Width,Height,Template,AreaContent,CreateTime ");
            strSql.Append(" FROM Public_AdAreaInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return SqlHelper.ExecuteDataset(WebConfig.SqlConnString, CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM Public_AdAreaInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = SqlHelper.ExecuteNonQuery(WebConfig.SqlConnString, CommandType.Text, strSql.ToString());
            return Common.TypeParse.ObjectToInt(obj, 0);
        }

        #endregion
    }
}
